<?php

if ($_SERVER['QUERY_STRING'] == '')
    die('Wrong Entry Point !!!');

define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE . DS . "include" . DS . "arrays.php");
require_once(JPATH_BASE . DS . "include" . DS . "setup.php");
require_once(JPATH_BASE . DS . "include" . DS . "functions.php");


if ($_GET['document_id'] == 2) {
    $sql = "select road_charge from companies where id = '" . $_GET['company_id'] . "'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $document_price = $row['road_charge'];
} else {
    $document_price = 0;
}

$sql = "select * from slabs where company_id = '" . $_GET['company_id'] . "' and slab_type = '" . $_GET['document_id'] . "' and slab_for = '" . $_GET['slab_for'] . "' and destination_id = '" . $_GET['destination_id'] . "'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count != 0) {
    $row = mysql_fetch_array($result);
} else {
    $sql = "select * from slabs where company_id = '" . $_GET['company_id'] . "' and slab_type = '" . $_GET['document_id'] . "' and slab_for = '" . $_GET['slab_for'] . "' and destination_id = '-1'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
}
$slab_id = $row['id'];
$additional_slab_weight = $row['additional_slab_weight'];
$additional_slab_unit = $row['additional_slab_unit'];
$additional_slab_local = $row['additional_slab_local'];
$additional_slab_state = ($row['additional_slab_state'] == 0.00) ? $row['additional_slab_region'] : $row['additional_slab_state'];
$additional_slab_region = $row['additional_slab_region'];
$additional_slab_rest = $row['additional_slab_rest'];
$additional_slab_zone5 = $row['additional_slab_zone5'];
$additional_slab_zone6 = $row['additional_slab_zone6'];
$additional_slab_zone7 = $row['additional_slab_zone7'];
$additional_slab_zone8 = $row['additional_slab_zone8'];
$additional_slab_zone9 = $row['additional_slab_zone9'];
$additional_slab_zone10 = $row['additional_slab_zone10'];
$additional_slab_zone11 = $row['additional_slab_zone11'];
$additional_slab_zone12 = $row['additional_slab_zone12'];

if ($additional_slab_unit != $_GET['weight_unit'])
    $additional_slab_weight = ($_GET['weight_unit'] == 1) ? ($additional_slab_weight * 1000) : ($additional_slab_weight / 1000);

$sql = "select * from slabdetails where slab_id = '" . $slab_id . "' order by id";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $slab_unit = $row['slab_unit'];
    $slab_weight = $row['slab_weight'];
    $slab_local_arr[$row['id']] = $row['slab_local'];
    $slab_state_arr[$row['id']] = ($row['slab_state'] == 0.00) ? $row['slab_region'] : $row['slab_state'];
    $slab_region_arr[$row['id']] = $row['slab_region'];
    $slab_rest_arr[$row['id']] = $row['slab_rest'];
    $slab_zone5_arr[$row['id']] = $row['slab_zone5'];
    $slab_zone6_arr[$row['id']] = $row['slab_zone6'];
    $slab_zone7_arr[$row['id']] = $row['slab_zone7'];
    $slab_zone8_arr[$row['id']] = $row['slab_zone8'];
    $slab_zone9_arr[$row['id']] = $row['slab_zone9'];
    $slab_zone10_arr[$row['id']] = $row['slab_zone10'];
    $slab_zone11_arr[$row['id']] = $row['slab_zone11'];
    $slab_zone12_arr[$row['id']] = $row['slab_zone12'];

    if ($slab_unit != $_GET['weight_unit']) {
        $slab_weight_arr[$row['id']] = ($_GET['weight_unit'] == 1) ? ($slab_weight * 1000) : ($slab_weight / 1000);
    } else {
        $slab_weight_arr[$row['id']] = $slab_weight;
    }
}


$sql = "select destination_type from destinations where id = '" . $_GET['destination_id'] . "'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$destination_type = $row['destination_type'];

switch ($destination_type) {
    case 1:
        $additional_slab_charge = $additional_slab_local;
        $slab_charge_arr = $slab_local_arr;
        break;

    case 2:
        $additional_slab_charge = $additional_slab_state;
        $slab_charge_arr = $slab_state_arr;
        break;

    case 3:
        $additional_slab_charge = $additional_slab_region;
        $slab_charge_arr = $slab_region_arr;
        break;

    case 4:
        $additional_slab_charge = $additional_slab_rest;
        $slab_charge_arr = $slab_rest_arr;
        break;

    case 5:
        $additional_slab_charge = $additional_slab_zone5;
        $slab_charge_arr = $slab_zone5_arr;
        break;

    case 6:
        $additional_slab_charge = $additional_slab_zone6;
        $slab_charge_arr = $slab_zone6_arr;
        break;

    case 7:
        $additional_slab_charge = $additional_slab_zone7;
        $slab_charge_arr = $slab_zone7_arr;
        break;

    case 8:
        $additional_slab_charge = $additional_slab_zone8;
        $slab_charge_arr = $slab_zone8_arr;
        break;

    case 9:
        $additional_slab_charge = $additional_slab_zone9;
        $slab_charge_arr = $slab_zone9_arr;
        break;
    case 10:
        $additional_slab_charge = $additional_slab_zone10;
        $slab_charge_arr = $slab_zone10_arr;
        break;
    case 11:
        $additional_slab_charge = $additional_slab_zone11;
        $slab_charge_arr = $slab_zone11_arr;
        break;
    case 12:
        $additional_slab_charge = $additional_slab_zone12;
        $slab_charge_arr = $slab_zone12_arr;
        break;
}


$slab_weight_select = implode('', array_slice($slab_weight_arr, 0, 1));
$slab_charge_select = implode('', array_slice($slab_charge_arr, 0, 1));

foreach ($slab_weight_arr as $k => $v)
    $allweight[] = $v;
foreach ($slab_charge_arr as $k => $v)
    $allcharges[] = $v;

#print_r($allweight);
#print_r($allcharges);
$get_weight = floatval($_GET['weight']);
#echo $get_weight;
#echo $additional_slab_weight;

$w0 = floatval($allweight[0]);
$w1 = floatval($allweight[1]);
$w2 = floatval($allweight[2]);

if ($w0 >= $get_weight) {
    $new_charge = $allcharges[0];
} else if ($w1 == 0 && $w0 <= $get_weight) {
    $finalweight = ceil(($get_weight - $w0)/ $additional_slab_weight);
    $new_charge = $allcharges[0] + ( $finalweight * $additional_slab_charge);
} else if ($w1 != 0 && $w1 >= $get_weight) {
    $new_charge = $allcharges[1];
} else if ($w1 != 0 && $w1 <= $get_weight) {
    $finalweight = ceil(($get_weight - $w1) / $additional_slab_weight);
    $new_charge = $allcharges[1] + ( $finalweight * $additional_slab_charge);
} else if ($w2 >= $get_weight) {
    $new_charge = $allcharges[2];
}



$return_arr = array();
$return_arr['fright'] = number_format($new_charge, 2, '.', '');
$return_arr['total'] = number_format($new_charge, 2, '.', '');
echo json_encode($return_arr);
?>